package Z.dailyExercise.March;

import java.util.Stack;

public class _1047删除字符串中的所有相邻重复项 {

    public static void main(String[] args) {
        _1047删除字符串中的所有相邻重复项 test = new _1047删除字符串中的所有相邻重复项();
        System.out.println(test.removeDuplicates("abbaca"));
    }


    /**
     * 借助栈操作
     * @param S
     * @return
     */
    public String removeDuplicates(String S) {

        Stack<Character> stack = new Stack<>();
        StringBuffer res = new StringBuffer();
        for (char c :S.toCharArray()){
            if (stack.isEmpty()){
                stack.push(c);
                continue;
            }

            if (stack.peek().equals(c)){
                stack.pop();
            }else {
                stack.push(c);
            }
        }

        for (char c:stack){
            res.append(c);
        }


        return res.toString();


    }
}
